package com.lqk.demo.atomic.fieldupdater;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/**
 * @author lqk
 * @date 2021/12/31 13:44
 *
 * AtomicReferenceFieldUpdater // 域 字段
 * AtomicIntegerFieldUpdater
 * AtomicLongFieldUpdater
 * 利用字段更新器，可以针对对象的某个域（Field）进行原子操作，只能配合 volatile 修饰的字段使用，否则会出现
 * 异常
 */
public class TestFieldUpdater1 {
    public static void main(String[] args) {
        Student student = new Student();
        AtomicReferenceFieldUpdater<Student, String> updater = AtomicReferenceFieldUpdater.newUpdater(Student.class,String.class,"name");
        updater.compareAndSet(student,null,"张三");
    }
}

class Student {
    volatile String name;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }
}
